﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using ERMap;

namespace PhoneBook.Users
{
    public class Users
    {
        public static Hashtable get(string userid)
        {
            Hashtable keys = new Hashtable();
            keys.Add("userid", userid);
            return ERMapHelper.getmodel("sys_users", keys);
        }

        public static Hashtable getbyemployee(string employeeid)
        {
            Hashtable keys = new Hashtable();
            keys.Add("employeeid", employeeid);
            return ERMapHelper.getmodel("sys_users", keys);
        }

        public static bool isExists(string username)
        {
            Hashtable olduser = getByName(username);
            return olduser != null;
        }

        public static Hashtable getByName(string username)
        {
            Hashtable keys = new Hashtable();
            keys.Add("username", username);
            return ERMapHelper.getmodel("sys_users", keys);
        }

        public static DataSet listgroupmanager(string groupid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select * from sys_users where groupid = @groupid and usertype in (2) and isnull(isdel,0) = 0");

            Hashtable param = new Hashtable();
            param["groupid"] = groupid;

            return ERMapHelper.getCustomData(sb.ToString(), param);
        }

        public static DataSet listgroup(string groupid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select * from sys_users where groupid = @groupid and usertype in (3) and isnull(isdel,0) = 0");

            Hashtable param = new Hashtable();
            param["groupid"] = groupid;

            return ERMapHelper.getCustomData(sb.ToString(), param);
        }

        public static DataSet listgroup(string groupid, string boid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select * from sys_users where groupid = @groupid and usertype in (3) and isnull(isdel,0) = 0 and bo = @boid");

            Hashtable param = new Hashtable();
            param["groupid"] = groupid;
            param["boid"] = boid;

            return ERMapHelper.getCustomData(sb.ToString(), param);
        }

        public static DataSet listmanager()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select * from sys_users where isnull(groupid,'') = '' and usertype in (0,1) and isnull(isdel,0) = 0");

            return ERMapHelper.getCustomData(sb.ToString(), null);
        }

        public static void addwebuser(Hashtable newuser)
        {
            newuser["usertype"] = 4;
            add(newuser);
        }

        public static void addgroupuser(Hashtable newuser)
        {
            newuser["usertype"] = 3;
            add(newuser);
        }

        public static void addgroupmanager(Hashtable newuser)
        {
            newuser["usertype"] = 2;
            add(newuser);
        }

        public static void addmanageruser(Hashtable newuser)
        {
            newuser["usertype"] = 1;
            add(newuser);
        }

        protected static void add(Hashtable newuser)
        {
            string userid = Commons.StringUtil.GetID_15();//string.Format("{0:yyMMdd}{1}", DateTime.Today, Commons.StringUtil.GetSYSSerialID(4));
            newuser["userid"] = userid;
            newuser["registertime"] = DateTime.Now;

            #region 密码先md5再使用3des加密
            Dx.ZcEncrypt.Encrypt3DES encrypt = new Dx.ZcEncrypt.Encrypt3DES();
            string userpwd = Commons.StrUtil.FormatValue(newuser["userpwd"]);
            userpwd = Dx.ZcEncrypt.Encrypt.GetMD5(userpwd);
            userpwd = encrypt.EncryptString(userpwd);
            newuser["userpwd"] = userpwd; 
            #endregion

            ERMapHelper.setmodel("sys_users", "userid", newuser);
        }

        public static void edit(Hashtable user)
        {
            if (user == null) return;

            #region 密码先md5再使用3des加密
            Dx.ZcEncrypt.Encrypt3DES encrypt = new Dx.ZcEncrypt.Encrypt3DES();
            string userpwd = Commons.StrUtil.FormatValue(user["userpwd"]);
            userpwd = Dx.ZcEncrypt.Encrypt.GetMD5(userpwd);
            userpwd = encrypt.EncryptString(userpwd);
            user["userpwd"] = userpwd;
            #endregion

            ERMapHelper.setmodel("sys_users", "userid", user);
        }

        public static void del(string userid)
        {
            Hashtable keys = new Hashtable();
            keys["userid"] = userid;
            ERMap.ERMapHelper.destroy("sys_users", keys);
        }
    }
}
